package com.openbravo.pos.customers;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SentenceExec;
import com.openbravo.data.loader.SentenceExecTransaction;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteBasicExt;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.SessionDB;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.BeanFactoryDataSingle;

public class DataLogicCustomers extends BeanFactoryDataSingle
{
  protected Session s;
  private TableDefinition tcustomers;
  private static Datas[] customerdatas = { Datas.STRING, Datas.TIMESTAMP, Datas.TIMESTAMP, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.INT, Datas.BOOLEAN, Datas.STRING };

  public void init(Session paramSession)
  {
    this.s = paramSession;
    this.tcustomers = new TableDefinition(paramSession, "CUSTOMERS", new String[] { "ID", "TAXID", "SEARCHKEY", "NAME", "NOTES", "VISIBLE", "CARD", "MAXDEBT", "CURDATE", "CURDEBT", "FIRSTNAME", "LASTNAME", "EMAIL", "PHONE", "PHONE2", "FAX", "ADDRESS", "ADDRESS2", "POSTAL", "CITY", "REGION", "COUNTRY", "TAXCATEGORY" }, new String[] { "ID", AppLocal.getIntString("label.taxid"), AppLocal.getIntString("label.searchkey"), AppLocal.getIntString("label.name"), AppLocal.getIntString("label.notes"), "VISIBLE", "CARD", AppLocal.getIntString("label.maxdebt"), AppLocal.getIntString("label.curdate"), AppLocal.getIntString("label.curdebt"), AppLocal.getIntString("label.firstname"), AppLocal.getIntString("label.lastname"), AppLocal.getIntString("label.email"), AppLocal.getIntString("label.phone"), AppLocal.getIntString("label.phone2"), AppLocal.getIntString("label.fax"), AppLocal.getIntString("label.address"), AppLocal.getIntString("label.address2"), AppLocal.getIntString("label.postal"), AppLocal.getIntString("label.city"), AppLocal.getIntString("label.region"), AppLocal.getIntString("label.country"), "TAXCATEGORY" }, new Datas[] { Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.BOOLEAN, Datas.STRING, Datas.DOUBLE, Datas.TIMESTAMP, Datas.DOUBLE, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING, Datas.STRING }, new Formats[] { Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.BOOLEAN, Formats.STRING, Formats.CURRENCY, Formats.TIMESTAMP, Formats.CURRENCY, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING, Formats.STRING }, new int[] { 0 });
  }

  public SentenceList getCustomerList()
  {
    return new StaticSentence(this.s, new QBFBuilder("SELECT ID, TAXID, SEARCHKEY, NAME FROM CUSTOMERS WHERE VISIBLE = " + this.s.DB.TRUE() + " AND ?(QBF_FILTER) ORDER BY NAME", new String[] { "TAXID", "SEARCHKEY", "NAME" }), new SerializerWriteBasic(new Datas[] { Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING }), new SerializerRead()
    {
      public Object readValues(DataRead paramDataRead)
        throws BasicException
      {
        CustomerInfo localCustomerInfo = new CustomerInfo(paramDataRead.getString(1));
        localCustomerInfo.setTaxid(paramDataRead.getString(2));
        localCustomerInfo.setSearchkey(paramDataRead.getString(3));
        localCustomerInfo.setName(paramDataRead.getString(4));
        return localCustomerInfo;
      }
    });
  }

  public int updateCustomerExt(final CustomerInfoExt paramCustomerInfoExt)
    throws BasicException
  {
    return new PreparedSentence(this.s, "UPDATE CUSTOMERS SET NOTES = ? WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
    {
      public void writeValues()
        throws BasicException
      {
    	  CustomerInfoExt  val$customer=paramCustomerInfoExt;
        setString(1, val$customer.getNotes());
        setString(2, val$customer.getId());
      }
    });
  }

  public final SentenceList getReservationsList()
  {
    return new PreparedSentence(this.s, "SELECT R.ID, R.CREATED, R.DATENEW, C.CUSTOMER, CUSTOMERS.TAXID, CUSTOMERS.SEARCHKEY, COALESCE(CUSTOMERS.NAME, R.TITLE),  R.CHAIRS, R.ISDONE, R.DESCRIPTION FROM RESERVATIONS R LEFT OUTER JOIN RESERVATION_CUSTOMERS C ON R.ID = C.ID LEFT OUTER JOIN CUSTOMERS ON C.CUSTOMER = CUSTOMERS.ID WHERE R.DATENEW >= ? AND R.DATENEW < ?", new SerializerWriteBasic(new Datas[] { Datas.TIMESTAMP, Datas.TIMESTAMP }), new SerializerReadBasic(customerdatas));
  }

  public final SentenceExec getReservationsUpdate()
  {
    return new SentenceExecTransaction(this.s)
    {
      public int execInTransaction(Object paramObject)
        throws BasicException
      {
        new PreparedSentence(DataLogicCustomers.this.s, "DELETE FROM RESERVATION_CUSTOMERS WHERE ID = ?", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0 })).exec(paramObject);
        if (((Object[])(Object[])paramObject)[3] != null)
          new PreparedSentence(DataLogicCustomers.this.s, "INSERT INTO RESERVATION_CUSTOMERS (ID, CUSTOMER) VALUES (?, ?)", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0, 3 })).exec(paramObject);
        return new PreparedSentence(DataLogicCustomers.this.s, "UPDATE RESERVATIONS SET ID = ?, CREATED = ?, DATENEW = ?, TITLE = ?, CHAIRS = ?, ISDONE = ?, DESCRIPTION = ? WHERE ID = ?", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0, 1, 2, 6, 7, 8, 9, 0 })).exec(paramObject);
      }
    };
  }

  public final SentenceExec getReservationsDelete()
  {
    return new SentenceExecTransaction(this.s)
    {
      public int execInTransaction(Object paramObject)
        throws BasicException
      {
        new PreparedSentence(DataLogicCustomers.this.s, "DELETE FROM RESERVATION_CUSTOMERS WHERE ID = ?", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0 })).exec(paramObject);
        return new PreparedSentence(DataLogicCustomers.this.s, "DELETE FROM RESERVATIONS WHERE ID = ?", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0 })).exec(paramObject);
      }
    };
  }

  public final SentenceExec getReservationsInsert()
  {
    return new SentenceExecTransaction(this.s)
    {
      public int execInTransaction(Object paramObject)
        throws BasicException
      {
        int i = new PreparedSentence(DataLogicCustomers.this.s, "INSERT INTO RESERVATIONS (ID, CREATED, DATENEW, TITLE, CHAIRS, ISDONE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?)", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0, 1, 2, 6, 7, 8, 9 })).exec(paramObject);
        if (((Object[])(Object[])paramObject)[3] != null)
          new PreparedSentence(DataLogicCustomers.this.s, "INSERT INTO RESERVATION_CUSTOMERS (ID, CUSTOMER) VALUES (?, ?)", new SerializerWriteBasicExt(DataLogicCustomers.customerdatas, new int[] { 0, 3 })).exec(paramObject);
        return i;
      }
    };
  }

  public final TableDefinition getTableCustomers()
  {
    return this.tcustomers;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.customers.DataLogicCustomers
 * JD-Core Version:    0.6.0
 */